package managers;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import components.Children;
import components.Component;
import features.Feature;
import features.FeatureList;

import ruletree.rule.Action;
import ruletree.rule.Rule;

public class RuleManager {

	public static final String INPUT = "input";
	public static final String OUTPUT = "output-actions";
	private static RuleManager ruleManager;
	private ArrayList<File> ruleFiles;
	private ArrayList<Rule> ruleList = new ArrayList<>();

	public static RuleManager getInstance() {
		if (ruleManager == null) {
			ruleManager = new RuleManager();
		}
		return ruleManager;
	}

	public void loadRules(ArrayList<File> xmlFiles) {
		ruleList = new ArrayList<>();
		for (int i = 0; i < xmlFiles.size(); i++) {
			ruleList.add(loadRule(xmlFiles.get(i)));
		}
	}

	public ArrayList<Action> loadRight(Element node) {
		ArrayList<Action> actionList = new ArrayList<>();
		List<Element> children = node.getChildren();
		
		for (Element child : children) {
			String name = child.getAttributeValue(Component.ATTRIBUTE_NAME);
			String var = child.getAttributeValue(Component.ATTRIBUTE_VAR);
			Action action = new Action(name);
			action.setVar(var);

				if (child.getChild("component") != null) {
					Children nodes = new Children(null);
					nodes.addChild(Component.createInstance(child.getChild("component"), false));
					action.setChildren(nodes);
				}

				if (child.getChild("features") != null) {
					FeatureList featurelist = new FeatureList(null);
					List<Element> features = child.getChild("features").getChildren();
					for (Element feature : features) {
						String atr = feature.getAttributeValue(Feature.ATTRIBUTE_NAME);
						String value = feature.getAttributeValue(Feature.ATTRIBUTE_VALUE);
						Feature feat = new Feature(atr, value, false);
						featurelist.setFeature(feat);
					}

					action.setFeatureList(featurelist);

				}
				actionList.add(action);
			}
		
		return actionList;
	}

	public Rule loadRule(File xmlFile) {
		SAXBuilder builder = new SAXBuilder();
		String ruleName = "";

		try {
			Document document = builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			ruleName = rootNode.getAttributeValue("name");
			Children child = new Children(null);
			Rule rule = new Rule(ruleName);

			// get the left hand side (node) of the rule
			Element leftNode = rootNode.getChild(INPUT);
			// get the right hand side (node) of the rule
			Element rightNode = rootNode.getChild(OUTPUT);

			List<Element> children = leftNode.getChildren();
			for (Element e : children) {
				child.addChild(Component.createInstance(e, false));
			}
			
			rule.setChildrenList(child);
			rule.setActionList(loadRight(rightNode));
			
			ruleList.add(rule); // added 

			return rule;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<Rule> getRuleList() {
		return ruleList;
	}

	public void setRuleList(ArrayList<Rule> ruleList) {
		this.ruleList = ruleList;
	}

	public Rule getSpecifiedRule(int index) {
		return ruleList.get(index);
	}
}
